package com.learn.demo.web;

import com.learn.demo.model.HttpResponseBody;
import com.learn.demo.model.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class HttpController {

    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
    static {
        for (int i =0; i< 10 ;i ++) {
            User user = new User();
            user.setId((long) i);
            user.setAge(i);
            user.setName("用户" + i);
            users.put((long) i, user);
        }
    }

    @ApiOperation(value="获取用户列表", notes="")
    @RequestMapping(value={"/getUserList"}, method= RequestMethod.GET)
    public HttpResponseBody<List<User>> getUserMap() {
        List<User> r = new ArrayList<User>(users.values());
        return new HttpResponseBody<>("200", "成功", r);
    }

    @ApiOperation(value="获取用户map", notes="")
    @RequestMapping(value={"/getMap"}, method= RequestMethod.POST)
    public HttpResponseBody<Map<Long, User>> getUserList() {
        return new HttpResponseBody<>("200", "成功", users);
    }

    @ApiOperation(value="根据id获取用户", notes="")
    @GetMapping("getById")
    public HttpResponseBody<User> getById(Long id) {
        return HttpResponseBody.successResponse("成功", users.get(id));
    }

    @ApiOperation(value="失败", notes="")
    @GetMapping("failed")
    public HttpResponseBody failed() {
        return HttpResponseBody.failResponse("失败");
    }
}
